<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Config;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    use HasFactory;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username',
        'realname',
        'nickname',
        'avatar',
        'email',
        'password',
        'ip',
        'status',
        'phone',
        'last_login_time',
        'sex',
        'birthday',
        'wechart',
        'wechart_name',
        'surname',
        'name',
        'zip_code',
        'institution',
        'institution_detail',
        'province_id',
        'city_id',
        'district_id',
        'address'
    ];

    public function receipt()
    {
        return $this->hasMany(Receipt::class);
    }


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        // 'realname',
        // 'avatar',
        // 'email',
        'email_verified_at',
        // 'created_at',
        // 'updated_at',
        'deleted_at',
        // 'ip',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'email_verified_at' => 'datetime',
        'institution_detail' => 'array'
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims(): array
    {
        return [];
    }

    //判断该手机号是否注册
    public static function checkPhone($phone = '')
    {
        return self::where('username', $phone)->orWhere('phone', $phone)->count();
    }


    /**
     * 当绑定手机号没有这个手机号码的时候去进行注册
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-07-06
     * @param array $payload
     * @return mixed
     */
    public static function weRegister($payload)
    {
        if (self::checkPhone($payload['phone']))  return ['status' => 0, 'msg' => "当前手机号已被注册"];
        $wx_info = WxloginMember::getOne(['openid' => $payload['open_id']]);

        $info = [
            'username' => $payload['phone'],
            'realname' => $wx_info['nickname'],
            'avatar' => $wx_info['headimgurl'],
            'password' => bcrypt(123456),
            'ip' => request()->ip(),
            'company_name' => $payload['company_name'],
            'phone' => $payload['phone'],
            'status' => 1,
        ];

        $member = self::create($info);

        if (!$member) return ['status' => 0, 'msg' => "注册失败"];
        noticeLog('用户注册成功' . json_encode($member));
        return $member;
    }


    public function order()
    {
        return $this->belongsTo(Order::class, 'user_id', 'id');
    }


    public function orderAmount()
    {
        return $this->hasMany(Order::class, 'user_id', 'id');
    }
}
